<style>
    #user-update {
        padding: 20px 25px 25px 0;
    }
    img {
        transition: transform 0.5s;
    }

    img:hover {
        -webkit-transform: scale(1.05, 1.05);
        -moz-transform: scale(1.05, 1.05);
        -ms-transform: scale(1.05, 1.05);
        -o-transform: scale(1.05, 1.05);
        transform: scale(1.05, 1.05);
    }
</style>
<div class="layui-fluid" id="user-update">
    <form class="layui-form" action="" lay-filter="user-update-form">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">形象照：</label>
            <div class="layui-upload">
                <div class="layui-upload-list">
                    <img class="layui-upload-img" src="" id="imgHead" name="imgHead" width="200px" height="150px">
                    <input type="hidden" id="headImgUrl" name="headImgUrl">
                    <p id="demoText3"></p >
                </div>
                <a id="uploadHead" style="padding-left: 110px">上传图片</a>
            </div>
        </div>
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">用户id：</label>
            <div class="layui-input-block">
                <input type="text" name="userId" data-th-value="${user.userId}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">用户名：</label>
            <div class="layui-input-block">
                <input type="text" name="username" minlength="4" maxlength="11" data-th-id="${user.userId}"
                       lay-verify="range|username" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">真实姓名：</label>
            <div class="layui-input-block">
                <input type="tel" name="realName" maxlength="10" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机：</label>
            <div class="layui-input-block">
                <input type="tel" name="mobile" lay-verify="phone"  maxlength="11" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">紧急手机：</label>
            <div class="layui-input-block">
                <input type="tel" name="emergencryCall" lay-verify="phone" maxlength="11" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">座机号码：</label>
            <div class="layui-input-block">
                <input type="tel" name="phone" placeholder="029-85988888" maxlength="15"  id="phone"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">微信号：</label>
            <div class="layui-input-block">
                <input type="tel" name="weChatNumber" id="weChatNumber" maxlength="30" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">民族：</label>
            <div class="layui-input-inline">
                <select name="nation" id="nation">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱：</label>
            <div class="layui-input-block">
                <input type="text" name="email" lay-verify="email" maxlength="50" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">角色：</label>
            <div class="layui-input-block" id="user-update-role"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门：</label>
            <div class="layui-input-block" id="user-update-dept"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">岗位：</label>
            <div class="layui-input-inline">
                <select name="postId" id="choPosition">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">状态：</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="有效">
                <input type="radio" name="status" value="0" title="禁用">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">性别：</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="0" title="男性">
                <input type="radio" name="sex" value="1" title="女性">
                <input type="radio" name="sex" value="2" title="保密">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数据权限：</label>
            <div class="layui-input-block" id="user-update-data-permission"></div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">自我介绍：</label>
            <div class="layui-input-block">
                <textarea name="description" maxlength="100" class="layui-textarea">
                </textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">座右铭：</label>
            <div class="layui-input-block">
                <textarea name="motto" maxlength="100" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="user-update-form-submit" id="submit"></button>
        </div>
    </form>
</div>

<script data-th-inline="javascript">
    layui.use(['febs', 'form', 'xmSelect', 'validate','upload'], function () {
        let $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            xmSelect = layui.xmSelect,
            user = [[${user}]],
            validate = layui.validate,
            dataPermissionXmlSelect,
            roleXmSelect,
            upload=layui.upload,
            deptXmlSelect;

        form.verify(validate);
        form.render();

        initUserValue();

        deptXmlSelect = xmSelect.render({
            el: '#user-update-dept',
            clickClose: true,
            model: {label: {type: 'text'}},
            tree: {
                show: true,
                strict: false,
                showLine: false,
                clickCheck: true,
                expandedKeys: [-1],
            },
            name: 'deptId',
            theme: {
                color: '#52c41a',
            },
            prop: {
                value: 'id'
            },
            height: 'auto',
            on: function (data) {
                if (data.isAdd) {
                    return data.change.slice(0, 1)
                }
            }
        });

        dataPermissionXmlSelect = xmSelect.render({
            el: '#user-update-data-permission',
            model: {label: {type: 'text'}},
            clickClose: true,
            tree: {
                show: true,
                strict: false,
                showLine: false,
                clickCheck: true,
                expandedKeys: [-1],
            },
            name: 'deptIds',
            theme: {
                color: '#52c41a',
            },
            prop: {
                value: 'id'
            },
            height: 'auto'
        });

        febs.get(ctx + 'dept/select/tree', null, function (data) {
            deptXmlSelect.update(data)
            dataPermissionXmlSelect.update(data)
            user.deptId && deptXmlSelect.setValue([user.deptId]);
            user.deptIds && dataPermissionXmlSelect.setValue(user.deptIds.split(","));
        });

        roleXmSelect = xmSelect.render({
            el: '#user-update-role',
            toolbar: {show: true},
            clickClose: true,
            name: 'roleId',
            theme: {
                color: '#32c787',
            },
            prop: {
                name: 'roleName',
                value: 'roleId'
            },
            data: []
        });

        febs.get(ctx + 'role', null, function (data) {
            roleXmSelect.update({
                data: data.data,
                autoRow: true,
            })
            roleXmSelect.setValue(user.roleId.split(','));
        });

        function initUserValue() {
            if(user.headImgUrl===null || user.headImgUrl===""){
                $('#imgHead').attr('src', "/febs/images/defaultImg.png");
            }else {
                $('#imgHead').attr('src', user.headImgUrl);
            }
            form.val("user-update-form", {
                "username": user.username,
                "realName": user.realName,
                "mobile": user.mobile,
                "email": user.email,
                "status": user.status,
                "sex": user.sex,
                "description": user.description,
                "emergencryCall": user.emergencryCall,
                "phone": user.phone,
                "weChatNumber": user.weChatNumber,
                "motto": user.motto,
                "headImgUrl": user.headImgUrl
            });
        }

        form.on('submit(user-update-form-submit)', function (data) {

            if (!verTel()) {
                console.log("333");
                febs.alert.info('请输入正确的座机号码');
                return false;
            }

            if (!data.field.roleId) {
                febs.alert.info('请选择用户角色');
                return false;
            }

            if (!data.field.roleId) {
                febs.alert.warn('请选择用户角色');
                return false;
            }
            if (febs.nativeEqual(data.field, user)) {
                febs.alert.warn('数据未作任何修改！');
                return false;
            }
            febs.post(ctx + 'user/update', data.field, function () {
                layer.closeAll();
                febs.alert.success(user.username + ' 用户数据修改成功');
                $('#febs-user').find('#query').click();
            });
            return false;
        });

        //个人形象照图片上传
        var uploadInst = upload.render({
            elem: '#uploadHead'
            ,url: ctx+'upload'
            ,method:'post'
            ,auto:false
            ,accept:'images'
            ,exts: 'jpg|png|gif|bmp|jpeg'
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                var flag = true;
                obj.preview(function(index, file, result){
                    var img = new Image();
                    img.src = result;
                    img.onload = function () { //初始化夹在完成后获取上传图片宽高，判断限制上传图片的大小。
                        if(img.width <=700 && img.height <=560){
                            $('#imgHead').attr('src', result); //图片链接（base64）
                            obj.upload(index, file); //满足条件调用上传方法
                        }else{
                            flag = false;
                            layer.msg("您上传的小图大小必须小于750*560尺寸！");
                            return false;
                        }
                    }
                    return flag;
                });
            }
            ,done: function(res){
                //上传成功
                if(res.code == 200){
                    layer.msg('上传图片成功');
                    $("#headImgUrl").val(res.url);
                    var url = $("#headImgUrl").val();
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

        //岗位的下拉菜单
        $.ajax({
            type:'get',
            url:'post/allPostList',
            cache:false,
            success:function (res) {
                // var postArray=res.data.rows
                // if(postArray.length>0){
                //     for (var i=0;i<postArray.length;i++){
                //         var postone=postArray[i];
                //         $("#choPosition").append("<option value='"+postone.postId+"'>"+postone.postName+"</option>")
                //     }
                // }
                let list = res.data;
                for (let i = 0; i < list.length; i++) {
                    $("#choPosition").append(new Option(list[i].postName, list[i].postId));
                }
                $("#choPosition").val(user.postId);
                layui.form.render("select");
            }
        });

        //民族的下拉菜单
        $.ajax({
            type:'get',
            url:'user/nationList',
            cache:false,
            success:function (res) {
                // var postArray=res.data.rows
                // if(postArray.length>0){
                //     for (var i=0;i<postArray.length;i++){
                //         var nation=postArray[i];
                //         $("#nation").append("<option value='"+nation.id+"'>"+nation.nationName+"</option>")
                //     }
                // }

                let list = res.data;
                for (let i = 0; i < list.length; i++) {
                    $("#nation").append(new Option(list[i].nationName, list[i].id));
                }
                $("#nation").val(user.nation);
                layui.form.render("select");
            }
        });
    });

    function verTel() {
        var tel=document.getElementById("phone").value;
        if (tel.length > 0) {
            if (!isPhone(tel)) {
                $("#phone").css({
                    border : "1px solid red"
                });
                return false;
            }
        }
        return true;
    }

    //固话
    function isPhone(val) {

        var sReg = /^[0][1-9]{2,3}-[0-9]{5,10}$/;
        var reg = new RegExp(sReg);
        console.log("222");
        console.log(reg.test(val));
        return reg.test(val);
    }
</script>